package com.ihighest.app.infox.bean;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;

import com.ihighest.app.common.kit.SharedPreferencesKit;


public class ProcessJson {

    static List<CityBean> cityList = new ArrayList<CityBean>();

    private static String result;

    public static String processJson(final Context context) {
        // // if (TextUtils.isEmpty(SharedPreferencesKit.getString(context,
        // "address"))) {
        // HttpUtils http = new HttpUtils();
        // http.send(HttpMethod.GET,
        // "http://data.infox.com/api/area_provincial_city_county.json", new
        // RequestCallBack<String>() {
        //
        // @Override
        // public void onLoading(long total, long current, boolean isUploading)
        // {
        // }
        //
        // @Override
        // public void onSuccess(ResponseInfo<String> responseInfo) {
        // System.out.println("结果" + responseInfo.result);
        // // SharedPreferencesKit.putString(context, "address",
        // responseInfo.result);
        // getDate(responseInfo.result.toString());
        //
        // }
        //
        // @Override
        // public void onStart() {
        // }
        //
        // @Override
        // public void onFailure(HttpException error, String msg) {
        // }
        // });
        // // } else {
        // // getDate(SharedPreferencesKit.getString(context, "address"));
        // // }
        try {
            // 设置请求的地址 通过URLEncoder.encode(String s, String enc)
            // 使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式
            String spec = "http://data.infox.com/api/area_provincial_city_county.json";
            // 根据地址创建URL对象(网络访问的url)
            URL url = new URL(spec);
            // url.openConnection()打开网络链接
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");// 设置请求的方式
            urlConnection.setReadTimeout(5000);// 设置超时的时间
            urlConnection.setConnectTimeout(5000);// 设置链接超时的时间
            // 设置请求的头
            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
            // 获取响应的状态码 404 200 505 302
            if (urlConnection.getResponseCode() == 200) {
                // 获取响应的输入流对象
                InputStream is = urlConnection.getInputStream();

                // 创建字节输出流对象
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                // 定义读取的长度
                int len = 0;
                // 定义缓冲区
                byte buffer[] = new byte[1024];
                // 按照缓冲区的大小，循环读取
                while ((len = is.read(buffer)) != -1) {
                    // 根据读取的长度写入到os对象中
                    os.write(buffer, 0, len);
                }
                // 释放资源
                is.close();
                os.close();
                result = new String(os.toByteArray());
                SharedPreferencesKit.putString(context, "address", result);
            } else {
                System.out.println("------------------链接失败-----------------");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    @SuppressWarnings("unused")
    public static List getDate(String json) {
        JSONArray array;

        String mJson = "";
        try {
            array = new JSONArray(json);

            for (int i = 0; i < array.length(); i++) {
                CityBean bean = new CityBean();
                bean.setId(array.getJSONObject(i).getString("id"));
                bean.setTitle(array.getJSONObject(i).getString("title"));
                bean.setAreacode(array.getJSONObject(i).getString("areacode"));
                JSONArray shenArray = new JSONArray(array.getJSONObject(i).getString("city"));
                // JSONArray shenArray =
                // array.getJSONObject(i).getJSONArray("city");
                List<ShengjiBean> sList = new ArrayList<ShengjiBean>();
                if (shenArray != null || shenArray.length() != 0) {

                    for (int j = 0; j < shenArray.length(); j++) {
                        ShengjiBean sheng = new ShengjiBean();
                        sheng.setId(shenArray.getJSONObject(j).getString("id"));
                        sheng.setTitle(shenArray.getJSONObject(j).getString("title"));
                        sheng.setAreacode(shenArray.getJSONObject(j).getString("areacode"));
                        JSONArray xianjiAry;
                        List<XianjiBean> xianji = new ArrayList<XianjiBean>();
                        if (shenArray.getJSONObject(j).has("county")) {
                            // 获取县级数据
                            xianjiAry = new JSONArray(shenArray.getJSONObject(j).getString("county"));
                            // xianjiAry =
                            // shenArray.getJSONObject(j).getJSONArray("county");
                            for (int k = 0; k < xianjiAry.length(); k++) {
                                XianjiBean xinBean = new XianjiBean();
                                xinBean.setId(xianjiAry.getJSONObject(k).getString("id"));
                                xinBean.setAreacode(xianjiAry.getJSONObject(k).getString("areacode"));
                                xinBean.setTitle(xianjiAry.getJSONObject(k).getString("title"));
                                xianji.add(xinBean);// 县级对象加入到县级数据集合中

                            }
                            sheng.setCounty(xianji);// 将县级数据放入省份对象上
                        } else {
                            sheng.setCounty(xianji);// 将县级数据放入省份对象上
                        }

                        sList.add(sheng);// 省份对象放入到省份集合中

                    }
                    bean.setCity(sList);// 将省份数据放入到升级对象中
                    cityList.add(bean);
                } else {
                    bean.setCity(sList);// 将省份数据放入到升级对象中
                    cityList.add(bean);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            System.out.print("异常：" + e.getMessage());
            e.printStackTrace();
        }

        return cityList;
    }
}
